﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Model;
using V5_Common;
using V5_BLL;
using System.Data;

namespace V5_EShop._Template.Default.Member
{
    public partial class Shopping_Address : BasicPage
    {
        public ModelUserToArea address = new ModelUserToArea();
        public string cities_1 = "", cities_2 = "", cities_3 = "";

        public string s1 = "", s2 = "", s3 = "";
        protected override void OnInit(EventArgs e)
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            base.OnInit(e);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            int id = Input.SetInt(Request.Form["hfid"]);
            int userid = user.ID;
            string ToUserName = txtToUserName.Text;
            string Provinces = Input.SetString(this.Tprovince.Value);
            string Cities = Input.SetString(this.TCity.Value);
            string Counties = Input.SetString(this.TCounty.Value);
            string UserToUrl = Input.SetString(txtUserToUrl.Text);
            string UserToPostNum = txtUserToPostNum.Text;
            string UserToPhone = txtUserToPhone.Text;
            string ToUserTelePhone = txtToUserTelePhone.Text;

            BLLUserToArea bll = new BLLUserToArea();
            ModelUserToArea model = new ModelUserToArea();
            model.tousername = ToUserName;
            model.touserareapro = Provinces;
            model.touserareacity = Cities;
            model.touserareacity2 = Counties;
            model.touserurl = UserToUrl;
            model.topostnum = UserToPostNum;
            model.touserphone = UserToPhone;
            model.tousertelephone = ToUserTelePhone;
            model.adddatetime = DateTime.Now;
            model.userid = userid;
            if (id == 0)
            {
                //检查一下最大的个数
                int count = bll.GetList(" userid=" + user.ID).Tables[0].Rows.Count;
                if (count < 4)
                {
                    model.id = bll.GetMaxId();
                    bll.Add(model);
                }
                else
                {
                    Pr.RegClientAlertAndRedirect(this, "只能添加4个地址!请删除一个在添加!", "/member/Shopping_Address.aspx");
                    return;
                }
            }
            else if (id > 0)
            {
                model.id = id;
                bll.Update(model);
                Pr.RegClientAlertAndRedirect(this, "恭喜您!修改成功!", "/member/Shopping_Address.aspx");
                return;
            }
            Js.Redirect("/member/Shopping_Address.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.check_member_login())
            {
                Response.Redirect("/login.aspx?returnurl=" + HTTPS.GetCurrentUrl());
            }
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["action"]);
                int id = Input.SetInt(Request["id"]);
                if (id > 0)
                {
                    address = new BLLUserToArea().GetModel(id);
                    txtToUserName.Text = address.tousername;
                    txtUserToUrl.Text = address.touserurl;
                    txtUserToPostNum.Text = address.topostnum;
                    txtUserToPhone.Text = address.touserphone;
                    txtToUserTelePhone.Text = address.tousertelephone;
                    s1 = address.touserareapro;
                    s2 = address.touserareacity;
                    s3 = address.touserareacity2;
                }
                //绑定地址
                //DataTable dt1 = new BLLCities().GetList(0, " area_parent_id='000000' ", " area_id Asc ").Tables[0];
                //foreach (DataRow row in dt1.Rows)
                //{
                //    cities_1 += "<option value=\"" + row["area_id"] + "\">" + row["area_name"] + "</option>";
                //}
                switch (Action)
                {
                    case "getcity":
                        string cityid = Input.SetString(Request["cityid"]);
                        Bind_City(cityid);
                        break;
                    default:
                        break;
                }
            }
            this.Bind_dlAreaList();
        }

        /// <summary>
        /// 获取城市
        /// </summary>
        /// <param name="city1"></param>
        private void Bind_City(string cityid)
        {
            DataTable dt2 = new BLLCities().GetList(0, " area_parent_id='" + cityid + "'", " area_id Asc ").Tables[0];
            cities_2 = "";
            foreach (DataRow row in dt2.Rows)
            {
                cities_2 += "<option value=\"" + row["area_id"] + "\">" + row["area_name"] + "</option>";
            }
            Response.Write(cities_2);
            Response.End();
        }

        private void Bind_dlAreaList()
        {
            this.dlHistoryList.DataSource = new BLLUserToArea().GetList(4, " userid=" + user.ID, " ID Desc ");
            this.dlHistoryList.DataBind();
        }

        protected void dlHistoryList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.ToLower().Trim())
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }

        private void Bind_DataEdit(int id)
        {
            Response.Redirect("/member/Shopping_Address.aspx?id=" + id);
        }

        private void Bind_Delete(int id)
        {
            new BLLUserToArea().Delete(id);
            Pr.RegClientAlertAndRedirect(this, "配货地址删除成功!", HTTPS.ComeUrl);
        }
    }
}
